package com.iflytek.exam.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.iflytek.exam.vo.ExamVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.iflytek.common.annotation.Log;
import com.iflytek.common.core.controller.BaseController;
import com.iflytek.common.core.domain.AjaxResult;
import com.iflytek.common.enums.BusinessType;
import com.iflytek.exam.domain.CreateExam;
import com.iflytek.exam.service.ICreateExamService;
import com.iflytek.common.utils.poi.ExcelUtil;
import com.iflytek.common.core.page.TableDataInfo;

/**
 * 创建考试基本信息Controller
 *
 * @author Dogdog
 * @date 2024-07-08
 */
@RestController
@RequestMapping("/exam/exam")
public class CreateExamController extends BaseController {
    @Autowired
    private ICreateExamService createExamService;

    /**
     * 判断输入的判断题，简答题等是否有题
     */
    @GetMapping("/sureIf")
    public TableDataInfo sureIf(ExamVo examVo) {
        String msg = createExamService.sureIf(examVo);
        return new TableDataInfo(msg);
    }

    /**
     * 查询创建考试基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('exam:exam:list')")
    @GetMapping("/list")
    public TableDataInfo list(CreateExam createExam) {
        startPage();
        List<CreateExam> list = createExamService.selectcreateExamListByUserId(createExam);
        return getDataTable(list);
    }

    /**
     * 教师查询创建的考试基本信息列表
     *
     * @param createExam
     * @return
     */
    @PreAuthorize("@ss.hasPermi('exam:exam:listBy')")
    @GetMapping("/listExamTwo")
    public TableDataInfo listExamTwo(CreateExam createExam) {
        startPage();
        List<CreateExam> list = createExamService.selectlistExamTwo(createExam);
        return getDataTable(list);
    }

    /**
     * 导出创建考试基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('exam:exam:export')")
    @Log(title = "创建考试基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CreateExam createExam) {
        List<CreateExam> list = createExamService.selectcreateExamList(createExam);
        ExcelUtil<CreateExam> util = new ExcelUtil<CreateExam>(CreateExam.class);
        util.exportExcel(response, list, "创建考试基本信息数据");
    }

    /**
     * 获取创建考试基本信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('exam:exam:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(createExamService.selectcreateExamById(id));
    }

    /**
     * 新增创建考试基本信息
     */
    @PreAuthorize("@ss.hasPermi('exam:exam:add')")
    @Log(title = "创建考试基本信息", businessType = BusinessType.INSERT)
    @PostMapping("/createExam")
    public TableDataInfo add(@RequestBody ExamVo examVo) {
        //获取用户输入接着开始进行
//        return toAjax(createExamService.insertcreateExam(createExam));
        List list = createExamService.insertExam(examVo);
        return getDataTable(list);
    }

    /**
     * 修改创建考试基本信息
     */
    @PreAuthorize("@ss.hasPermi('exam:exam:edit')")
    @Log(title = "创建考试基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CreateExam createExam) {
        return toAjax(createExamService.updatecreateExam(createExam));
    }

    /**
     * 删除创建考试基本信息
     */
    @PreAuthorize("@ss.hasPermi('exam:exam:remove')")
    @Log(title = "创建考试基本信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(createExamService.deletecreateExamByIds(ids));
    }

    /**
     * 进行试卷的分发
     */
    @Log(title = "进行试卷的分发", businessType = BusinessType.INSERT)
    @PostMapping("/distribute/{ids}/{uuid}")
    public AjaxResult distribute(@RequestBody ExamVo examVo, @PathVariable Long[] ids, @PathVariable String uuid) {
        //进行试卷的分发
        return toAjax(createExamService.distribute(examVo, ids, uuid));
    }

    /**
     * 删除试卷表，试卷—-权限关联表，试卷-教师关联表，试卷-题关联表
     */
    @Log(title = "删除试卷表", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteAllpaperManage/{id}")
    public TableDataInfo remove(@PathVariable Long id) {
        Boolean a = createExamService.deleteAllExam(id);
        String msg = a ? "删除成功" : "删除失败";
        return new TableDataInfo();
    }

}
